package sample.tangocho.activity;

import java.util.List;

import sample.tangocho.model.Tangocho;
import sample.tangocho.service.FindTangochoService;
import sample.tangocho.service.FindTangochoServiceImpl;
import sample.tangocho.service.RemoveTangochoService;
import sample.tangocho.service.RemoveTangochoServiceImpl;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * ホームアクティビティ。
 * 
 * @author
 * 
 */
public class HomeActivity extends ListActivity {

	// private CreateTangochoService createTangochoService;

	private FindTangochoService findTangochoService;

	private RemoveTangochoService removeTangochoService;

	private List<Tangocho> tangochoList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);

		// TODO DI
		// createTangochoService = new CreateTangochoServiceImpl(
		// getApplicationContext());
		findTangochoService = new FindTangochoServiceImpl(
				getApplicationContext());
		removeTangochoService = new RemoveTangochoServiceImpl(
				getApplicationContext());

		ListView listView = (ListView) findViewById(android.R.id.list);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ListView listView = (ListView) parent;
				// クリックされたアイテムを取得します
				Tangocho item = (Tangocho) listView.getItemAtPosition(position);

				Intent intent = new Intent(getApplicationContext(),
						ViewCardActivity.class);

				// TODO Tangocho　自体を渡す
				intent.putExtra("tangochoId", item.getId());

				startActivityForResult(intent, 1);
			}
		});

		listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				return false;
			}
		});

		view();
	}

	// /**
	// * 単語帳を作成。
	// *
	// * @param view
	// */
	// public void createTangocho(View view) {
	// EditText editTitle = (EditText) findViewById(R.id.edit_title);
	//
	// if (editTitle.getText().toString().length() > 0) {
	// createTangochoService
	// .createTangocho(editTitle.getText().toString());
	// editTitle.setText("");
	// }
	//
	// view();
	// }

	private void view() {

		ArrayAdapter<Tangocho> adapter = new ArrayAdapter<Tangocho>(this,
				android.R.layout.simple_list_item_1);

		tangochoList = findTangochoService.findTangochoList();

		for (Tangocho tangocho : tangochoList) {
			// アイテムを追加します
			adapter.add(tangocho);
		}

		ListView listView = (ListView) findViewById(android.R.id.list);

		// アダプターを設定します
		listView.setAdapter(adapter);

		// コンテキストメニューのためにリストビューを登録
		registerForContextMenu(listView);

	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {

		// コンテキストメニューを作る
		menu.add(0, 1, 0, "タイトル変更");
		menu.add(0, 2, 0, "削除");

		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {

		final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();

		final Tangocho tangocho = tangochoList.get(info.position);

		switch (item.getItemId()) {
		case 1:
			Intent intent = new Intent(getApplicationContext(),
					EditTangochoActivity.class);

			// TODO Tangocho　自体を渡す
			intent.putExtra("tangochoId", tangocho.getId());
			intent.putExtra("tangochoTitle", tangocho.getTitle());

			startActivityForResult(intent, 1);
			break;
		case 2:
			new AlertDialog.Builder(this)
					.setMessage("「" + tangocho.getTitle() + "」を削除しますか?")
					.setPositiveButton("Yes",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									removeTangochoService
											.removeTangocho(tangocho.getId());
									tangochoList.remove(info.position);
									view();
								}
							})
					.setNegativeButton("No",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									/* ここにNOの処理 */
								}
							}).show();
			break;
		default:
			break;
		}

		view();
		return super.onContextItemSelected(item);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		// XMLからメニュー定義を取得し、メニューに設定
		getMenuInflater().inflate(R.menu.home_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Intent intent = null;

		switch (item.getItemId()) {
		case R.id.create_tangocho:
			intent = new Intent(getApplicationContext(),
					EditTangochoActivity.class);
			break;
		case R.id.import_tangocho:
			intent = new Intent(getApplicationContext(),
					ImportTangochoActivity.class);
		default:
			// 処理なし
		}

		startActivityForResult(intent, 1);
		return true;
	}

	@Override
	public void onResume() {
		super.onResume();

		view();

	}
}